package com.dragon.english_practice_back.controller;

import com.dragon.english_practice_back.reponse.Result;
import com.dragon.english_practice_back.service.AuthenticationUtil;
import com.dragon.english_practice_back.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/userInfo")
public class UserInfoController {
    @Autowired
    AuthenticationUtil authenticationUtil;
    @Autowired
    UserService userService;

    //修改用户名
    @PutMapping("/updateName")
    public Result updateName(@RequestParam  String name){
        //获取当前用户的id
        int id = authenticationUtil.id();
        log.info("/userInfo/updateName id:{} newName: {}", id, name);
        //修改用户名
        if (userService.updateName(id, name)){
            return Result.ok("修改成功" , name);
        };
        return Result.no("修改失败");
    }

    //修改密码
    @PutMapping("/updatePassword")
    public Result updatePassword(@RequestParam  String password){
        //获取当前用户的id
        int id = authenticationUtil.id();
        log.info("/userInfo/updatePassword id:{} newPassword: {}", id, password);
        //修改密码
        if (userService.updatePassword(id, password)){
            return Result.ok("修改成功");
        };
        return Result.no("修改失败");
    }

}
